﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class WinForm : Form
    {
        public WinForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void WinForm_Load(object sender, EventArgs e)
        {

            var select = string.Format("select*from Load");
            var res = DbHelper.GetDataTable(select);
            dataGridView1.DataSource = res;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var name = this.textBox1.Text;
            var result = string.Format("select*from Load where  Name like'%{0}%'",name);
            var res = DbHelper.GetDataTable(result);
             dataGridView1.DataSource=res;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            EditForm editForm = new EditForm();
            var res = editForm.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var name = this.textBox1.Text;
                var insert = string.Format("select * from Load where Name like'%{0}%'", name);
                var data = DbHelper.GetDataTable(insert);
                dataGridView1.DataSource = data;
            }
            else 
            {
                MessageBox.Show("添加失败");
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
                var pwd = (string)dataGridView1.SelectedRows[0].Cells["Password"].Value;
                EditForm editForm = new EditForm(id, name, pwd);
                var res = editForm.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    var nameTemp = this.textBox1.Text;
                    var conString = string.Format("select*from Load where Name like '%{0}%'", nameTemp);
                    var dt = DbHelper.GetDataTable(conString);
                    dataGridView1.DataSource = dt;
                   
                }
                else
                {
                    MessageBox.Show("取消添加");
                }



            }
            else 
            {
                MessageBox.Show("未选择信息进行更新","提示");
            }
            
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;
                var res = MessageBox.Show("确认删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Load where Id={0}", id);
                    var rowcount = DbHelper.AddOrDeleteOrEdittOrSave(sql);
                    if (rowcount > 0)
                    {
                        var name = this.textBox1.Text;
                        var insert = string.Format("select*from Load where Name like '%{0}%'", name);
                        var dt = DbHelper.GetDataTable(insert);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功", "提示");
                    }

                }
            }

            else
            {
                MessageBox.Show("没有选择行数进行删除", "提示");
            }
            }
            
        }
    }

